package com.lijin.workshop.event.upopwkrout20131122;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lijin.workshop.util.FileLineHandler;
import com.lijin.workshop.util.FileUtil;

public class PRODUponlRoutImporter {

	private static final String BASE_DIR = "D:\\工作目录\\02.日常工作\\生产事件处理\\2013\\20131119-批导无卡路由\\";
	
	public static void main(String[] args) {
		
		String prodFile = BASE_DIR + "UPONL_ROUTER_PROD.txt";
		final List<Map<String, String>> chnlLst = new ArrayList<Map<String, String>>();
		final String[] routHeader = new String[11];
		FileUtil.readFileByLine(prodFile, "GBK", new FileLineHandler() {
			@Override
			public void handleLine(String line) {
				String[] strs = line.split("[|]");
				Map<String, String> m = new HashMap<String, String>();
				for (int i = 0; i < strs.length; i ++) {
					if (line.contains("ISS_HEAD")) {
						routHeader[i] = strs[i].trim();
					} else {
						m.put(routHeader[i], strs[i].trim());
					}
				}
				if (!line.contains("ISS_HEAD")) {
					chnlLst.add(m);
				}
			}
		});
		
		List<String> sqls = new ArrayList<String>();
		for (Map<String, String> m : chnlLst) {
			StringBuilder sql = new StringBuilder("insert into TBL_UPONL_ROUTER_DET" +
					" (ISS_HEAD, ISS_INS_ID_CD, CARD_ATTR, TRANS_ID, BUS_TP, MCHNT_CLASS, RCV_TP, CHANNEL, RCV_INS_ID_CD, PROC_SYS, REC_ST, REC_UPD_TS, REC_CRT_TS, COMMENTS)" +
					" values (");
			sql.append("'" + m.get("ISS_HEAD") + "',");
			sql.append("'" + m.get("ISS_INS_ID_CD") + "',");
			sql.append("'" + m.get("CARD_ATTR") + "',");
			sql.append("'" + m.get("TRANS_ID") + "',");
			sql.append("'" + m.get("BUS_TP") + "',");
			sql.append("'" + m.get("MCHNT_CLASS") + "',");
			sql.append("'" + m.get("RCV_TP") + "',");
			sql.append(m.get("CHANNEL") + ",");
			sql.append("'" + m.get("RCV_INS_ID_CD") + "',");
			sql.append("'" + m.get("PROC_SYS") + "',");
			sql.append("'" + m.get("REC_ST") + "',");
			sql.append("current timestamp,");
			sql.append("current timestamp,");
			sql.append("'');");
			sqls.add(sql.toString());
		}
		
		String prodImportFile = BASE_DIR + "UPONL_ROUTER_PROD_import.sql";
		FileUtil.writeFileByLine(new File(prodImportFile), sqls, "GBK");
	}
	
}
